﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class PlanDao : DataAccessObject
    {
        #region Add

        public void AddPlan(Plan plan)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddToplan(plan);
                    
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyPlan(Plan plan)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    Plan oldPlan = entities.plan.FirstOrDefault(e => e.planId == plan.planId);
                    oldPlan = plan;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Get

        public List<Plan> GetPlanes(int financiadorId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.plan
                                where e.financiadorId == financiadorId
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
